<template>
  <a-card :bordered="false">

    <a-spin :spinning="confirmLoading">

      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button @click="clearWxJxCache" type="primary" icon="plus">清空小程序缓存</a-button>
        <a-button @click="clearManagerCache" type="primary" icon="plus">清空后端缓存</a-button>
      </div>


      <a-form :form="form">


        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="发货超过">
          <a-input-number v-decorator="[ 'confirmOvertime', {}]"/>
          <span class="note-margin">【天】，未收货，订单自动完成</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="初审后">
          <a-input-number v-decorator="[ 'normalOrderOvertime', {}]"/>
          <span class="note-margin">【小时】未复审，订单自动取消</span>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="物流到货后">
          <a-input-number v-decorator="[ 'finishOvertime', {}]"/>
          <span class="note-margin">【天】未结案，发送消息提醒</span>
        </a-form-item>
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="退货限制时间(天)">-->
<!--          <a-input-number v-decorator="[ 'returnsOvertime', {}]"/>-->
<!--        </a-form-item>-->
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="下单是否判断库存">-->
<!--          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="limitStockFlag"/>-->
<!--        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否允许无库存下单">
          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="orderCreateStockFlag"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否允许无库存审核订单">
          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="orderAuditStockFlag"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否允许余额不足审核订单">
          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="orderAuditBalanceFlag"/>
        </a-form-item>
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="下单是否判断最小下单金额和箱数">-->
<!--          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="limitMinOrderFlag"/>-->
<!--        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开启短信提醒">
          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="enableSms"/>
        </a-form-item>
<!--        <a-form-item-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          label="开启微信提醒">-->
<!--          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="enableWx"/>-->
<!--        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="最大折扣占比输入（%）">
          <a-input-number v-decorator="[ 'discountLimit', {}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="是否允许订单结案">
          <a-switch checkedChildren="是" unCheckedChildren="否" v-model="orderCloseFlag"/>
        </a-form-item>
        <a-form-item :wrapperCol="{span: 24, offset: 10}">
          <a-button type="primary" @click="handleOk">提交</a-button>
        </a-form-item>

      </a-form>
    </a-spin>

  </a-card>
</template>

<script>
import {getAction} from '@/api/manage'
import {httpAction} from '@/api/manage'
import pick from 'lodash.pick'
import moment from "moment"

export default {
  name: "OmsOrderSettingList",
  components: {},
  data() {
    return {
      description: '订单设置管理页面',
      model: {},
      limitStockFlag: true,
      limitMinOrderFlag: true,
      enableSms: true,
      toSapFlag: true,
      orderAuditStockFlag: true,
      orderCreateStockFlag: true,
      orderAuditBalanceFlag: true,
      orderCloseFlag:true,
      enableWx: true,
      labelCol: {
        xs: {span: 24},
        sm: {span: 9},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 12},
      },
      confirmLoading: false,
      form: this.$form.createForm(this),
      url: {
        list: "/oms/omsOrderSetting/queryOne",
        add: "/oms/omsOrderSetting/add",
        edit: "/oms/omsOrderSetting/edit",
        clearWxJxCacheUrl: 'oms/omsOrder/clearWxJxCache',
        clearManageCacheUrl: 'oms/omsOrder/clearManageCache',
      },
    }
  },
  created() {
    this.loadData();
  },
  computed: {},
  methods: {
    loadData() {
      this.form.resetFields();
      getAction(this.url.list).then((res) => {
        if (res.success) {
          this.model = res.result;
          this.limitStockFlag = res.result.limitStockFlag == 1 ? true : false;
          this.limitMinOrderFlag = res.result.limitMinOrderFlag == 1 ? true : false;
          this.enableSms = res.result.enableSms == 1 ? true : false;
          this.toSapFlag = res.result.toSapFlag == 1 ? true : false;
          this.enableWx = res.result.enableWx == 1 ? true : false;
          this.orderAuditBalanceFlag = res.result.orderAuditBalanceFlag == 1 ? true : false;
          this.orderCreateStockFlag = res.result.orderCreateStockFlag == 1 ? true : false;
          this.orderAuditStockFlag = res.result.orderAuditStockFlag == 1 ? true : false;
          this.orderCloseFlag = res.result.orderCloseFlag == 1 ? true : false;
        } else {
          this.model = {};
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'confirmOvertime'))
          this.form.setFieldsValue(pick(this.model, 'normalOrderOvertime'))
          this.form.setFieldsValue(pick(this.model, 'finishOvertime'))
          this.form.setFieldsValue(pick(this.model, 'discountLimit'))
          //时间格式化
        });
      })
    },

    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);

          formData.limitStockFlag = this.limitStockFlag ? 1 : 0;
          formData.limitMinOrderFlag = this.limitMinOrderFlag ? 1 : 0;
          formData.enableSms = this.enableSms ? 1 : 0;
          formData.toSapFlag = this.toSapFlag ? 1 : 0;
          formData.enableWx = this.enableWx ? 1 : 0;
          formData.orderAuditBalanceFlag = this.orderAuditBalanceFlag ? 1 : 0;
          formData.orderCreateStockFlag = this.orderCreateStockFlag ? 1 : 0;
          formData.orderAuditStockFlag = this.orderAuditStockFlag ? 1 : 0;
          formData.orderCloseFlag = this.orderCloseFlag ? 1 : 0;
          console.log(formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      })
    },
    clearWxJxCache() {
      let params = {}
      getAction(this.url.clearWxJxCacheUrl, params).then((res) => {
        if (res.success) {
          this.$message.success("完成清理")
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
      })
    },
    clearManagerCache() {
      let params = {}
      getAction(this.url.clearManageCacheUrl, params).then((res) => {
        if (res.success) {
          this.$message.success("完成清理")
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
      })
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less'
</style>